<?php

class crop{
        protected  $cropID;
        public function __construct() {
            $args = func_get_args();
            if(count($args)==1){
               $this->setCropID($args[0]); 
            }  else {
                $this->addCrop($args[0], $args[1], $args[2]);
            }
        }

        private function setCropID($cropID){ 
            $this->cropID=$cropID;
        }
        
        public function getCropID(){
            return $this->cropID;
        }
                
        public function getCrop(){
            $mysqli=  connect();
            $query="
                select  *
                from    member_farm_crop
                where   cropID='".$this->getCropID()."'
                ";
            $result=  $mysqli->query($query);
            $row=  $result->fetch_array(MYSQLI_ASSOC);
            return $row;
        }
        public function getCost($costID){
            $mysqli=  connect();
            $query="
                select  *
                from    crop_cost
                where   cropID='".$this->getCropID()."' and
                        costID='".$costID."'
                ";
            $result=  $mysqli->query($query);
            $row=  $result->fetch_array(MYSQLI_ASSOC);
            return $row["cost"];
        }
        public function getIncome($incomeID){
            $mysqli=  connect();
            $query="
                select  *
                from    crop_income
                where   cropID='".$this->getCropID()."' and
                        incomeID='".$incomeID."'
                ";
            $result=  $mysqli->query($query);
            $row=  $result->fetch_array(MYSQLI_ASSOC);
            return $row["income"];
        }
        function setCost($costID,$cost){
            $mysqli=  connect();
            
            if($cost=="" || $cost==0){
                $query="
                    delete from crop_cost
                    where       cropID='".$this->getCropID()."' and
                                costID='".$costID."'
                    ";
                $result=$mysqli->query($query);
            }else if($this->checkCost($costID)){
                $query="
                    update  crop_cost
                    set     cost='".$cost."'
                    where   cropID='".$this->getCropID()."' and
                            costID='".$costID."'
                    ";
                $result=$mysqli->query($query);
            }else{
                $query="
                    insert into crop_cost
                        (
                            costID,
                            cropID,
                            cost
                        ) values (
                            '".$costID."',
                            '".$this->cropID."',
                            '".$cost."'
                        )
                    ";
                $result=$mysqli->query($query);
            }
        }
        public function setIncome($incomeID,$income){
            $mysqli=  connect();
            
            if($income=="" || $income==0){
                $query="
                    delete from crop_income
                    where       cropID='".$this->getCropID()."' and
                                incomeID='".$incomeID."'
                    ";
                $result=$mysqli->query($query);
            }else if($this->checkIncome($incomeID)){
                $query="
                    update  crop_income
                    set     income='".$income."'
                    where   cropID='".$this->getCropID()."' and
                            incomeID='".$incomeID."'
                    ";
                $result=$mysqli->query($query);
            }else{
                $query="
                    insert into crop_income
                        (
                            incomeID,
                            cropID,
                            income
                        ) values (
                            '".$incomeID."',
                            '".$this->cropID."',
                            '".$income."'
                        )
                    ";
                $result=$mysqli->query($query);
            }
        }
        public function checkCost($costID){
            $mysqli=  connect();
            $query="
                select  *
                from    crop_cost
                where   costID='".$costID."' and
                        cropID='".$this->cropID."'
                ";
            $result=$mysqli->query($query);
            if($result->num_rows>0){
                return TRUE;
            }else{
                return FALSE;
            }
        }
        public function checkIncome($incomeID){
            $mysqli=  connect();
            $query="
                select  *
                from    crop_income
                where   incomeID='".$incomeID."' and
                        cropID='".$this->cropID."'
                ";
            $result=$mysqli->query($query);
            if($result->num_rows>0){
                return TRUE;
            }else{
                return FALSE;
            }
        }
        public function addCrop($farmID,$startDate,$endDate,$rvID){
            $mysqli=  connect();
            $query="
                insert into member_farm_crop
                    (
                        farmID,
                        startDate,
                        endDate,
                        rvID
                    ) values (
                        '".$farmID."',
                        '".$startDate."',
                        '".$endDate."',
                        '".$rvID."'
                    )
                ";
            $result=$mysqli->query($query);
        }
        public function  updateCrop($input){
            $mysqli=  connect();
            echo $query="
                update  member_farm_crop
                set     startDate='".$input["startDate"]."',
                        endDate='".$input["endDate"]."',
                        rvID='".$input["rvID"]."'
                where   cropID='".$this->getCropID()."'
                ";
            $mysqli->query($query);
            return $this->getCrop();
        }
        
        public function sumCost(){
            $mysqli=  connect();
            $args = func_get_args();
            if(count($args)==0){
                $query="
                    select  sum(cost) as 'sumCost'
                    from    crop_cost
                    where   cropID='".$this->cropID."'
                    ";
                $result=$mysqli->query($query);
                $row=$result->fetch_array(MYSQLI_ASSOC);
                return $row["sumCost"];
            }else{
                $costID=$args[0];
                $query="
                    select  sum(cost) as 'sumCost'
                    from    crop_cost
                    where   cropID='".$this->cropID."' and
                            costID in (
                                select  costID
                                from    cost_list
                                where   costID='".$costID."' or
                                        parent='".$costID."'
                            )
                    ";
                $result=$mysqli->query($query);
                $row=$result->fetch_array(MYSQLI_ASSOC);
                return $row["sumCost"];
            }
        }
        
        public function percentCost($costID){
            $mysqli=  connect();
            $args = func_get_args();
            $allCost=  $this->sumCost();
            if($allCost==0){
                return 0;
            }else{
                $sumCost= $this->sumCost($costID);
                return ($sumCost*100)/$allCost;
            }
        }
        
        public function percentSubCost($parentCostID, $costID){
            $mysqli=  connect();
            $args = func_get_args();
            $allCost=  $this->sumCost($parentCostID);
            if($allCost==0){
                return 0;
            }else{
                $sumCost= $this->sumCost($costID);
                return ($sumCost*100)/$allCost;
            }
        }
        
        public function sumIncome(){
            $mysqli=  connect();
            $query="
                select  sum(income) as 'sumIncome'
                from    crop_income
                where   cropID='".$this->cropID."'
                ";
            $result=$mysqli->query($query);
            $row=$result->fetch_array(MYSQLI_ASSOC);
            return $row["sumIncome"];
        }
        
        public function profit(){
            return $this->sumIncome()-$this->sumCost();
        }
        
        public function delCrop(){
            $mysqli=  connect(); 
            if($this->isCostInCrop()){
                $query="
                delete  member_farm_crop
                where   cropID='".$this->cropID."'
                ";
            $mysqli->query($query);
            }  else {
                
            $query="
                delete from crop_cost
                where   cropID='".$this->cropID."'
                ";
                $mysqli->query($query);
               // $this->delCrop($cropID);
            $query="
                delete from member_farm_crop
                where   cropID='".$this->cropID."'
                ";
            $mysqli->query($query);
            }
        }
        
        private function  isCostInCrop(){
            $mysqli=  connect(); 
            $query="
                   select * 
                   from crop_cost
                   where cropID='".$cropID."'
                   ";
            $result=$mysqli->query($query);
            if($result->num_rows==0){
                return FALSE;
            }else{
                return TRUE;
            }
        }
    }
